package com.thwj.project.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.thwj.common.annotation.Log;
import com.thwj.common.core.controller.BaseController;
import com.thwj.common.core.domain.AjaxResult;
import com.thwj.common.core.page.TableDataInfo;
import com.thwj.common.enums.BusinessType;
import com.thwj.common.utils.poi.ExcelUtil;
import com.thwj.project.biz.domain.Area;
import com.thwj.project.biz.service.IAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 行政区域-来源于百度行政baidu_areaController
 *
 * @author generate
 * @date 2023-06-16
 */
@RestController
@RequestMapping("/biz/area")
public class AreaController extends BaseController {
    @Autowired
    private IAreaService areaService;

/**
 * 查询行政区域-来源于百度行政baidu_area列表
 */
@PreAuthorize("@ss.hasPermi('biz:area:list')")
@GetMapping("/list")
    public TableDataInfo list(Area area) {
        startPage();
        List<Area> list = areaService.list(new QueryWrapper<>(area));
        return getDataTable(list);
    }

    /**
     * 导出行政区域-来源于百度行政baidu_area列表
     */
    @PreAuthorize("@ss.hasPermi('biz:area:export')")
    @Log(title = "行政区域-来源于百度行政baidu_area", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Area area) {
        List<Area> list = areaService.list(new QueryWrapper<>(area));
        ExcelUtil<Area> util = new ExcelUtil<Area>(Area. class);
        util.exportExcel(response, list, "行政区域-来源于百度行政baidu_area数据");
    }

    /**
     * 获取行政区域-来源于百度行政baidu_area详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:area:query')")
    @GetMapping(value = "/{areaCode}")
    public AjaxResult getInfo(@PathVariable("areaCode") Long areaCode) {
        return AjaxResult.success(areaService.getById(areaCode));
    }

    /**
     * 新增行政区域-来源于百度行政baidu_area
     */
    @PreAuthorize("@ss.hasPermi('biz:area:add')")
    @Log(title = "行政区域-来源于百度行政baidu_area", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Area area) {
        return toAjax(areaService.save(area));
    }

    /**
     * 修改行政区域-来源于百度行政baidu_area
     */
    @PreAuthorize("@ss.hasPermi('biz:area:edit')")
    @Log(title = "行政区域-来源于百度行政baidu_area", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Area area) {
        return toAjax(areaService.updateById(area));
    }

    /**
     * 删除行政区域-来源于百度行政baidu_area
     */
    @PreAuthorize("@ss.hasPermi('biz:area:remove')")
    @Log(title = "行政区域-来源于百度行政baidu_area", businessType = BusinessType.DELETE)
    @DeleteMapping("/{areaCodes}")
    public AjaxResult remove(@PathVariable Long[] areaCodes) {
        return toAjax(areaService.removeByIds(Arrays.asList(areaCodes)));
    }

    /**
     * 查询行政区域-来源于百度行政baidu_area列表
     */
    @PreAuthorize("@ss.hasPermi('biz:area:list')")
    @GetMapping("/listCity")
    public AjaxResult listCity(Area area) {
        List<Area> list = areaService.list(new QueryWrapper<Area>()
                .groupBy(Arrays.asList("city_code","city_name")).select("city_code as cityCode,city_name as cityName")
        );
        return AjaxResult.success(list);
    }

}
